<!DOCTYPE html>
<html style="height: 100%">

<head>
    <meta charset="utf-8">
    <input id="jsonName">
    <p style="font-size: 20px;color: red;">输入具有具体地址的csv文件名导出带有WGS84坐标的csv文件</p>
    <button onclick='tableToExcel()'>导出</button>
</head>

<body style="height: 100%; margin: 0">
    <div id="container" style="height: 100%"></div>
    <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.12&key=5ee4c9a2f2224e3264c8a8e905161764&plugin=AMap.Geocoder"></script>
    <script type="text/javascript" src="coordsConvert.js"></script>
    <script type="text/javascript">
        function tableToExcel() {
            $.get('json/' + $("#jsonName").val(), function (data) {
                var points = [];
                var coordsData = [].concat.apply([], data.map(function (busLine, idx) {
                    var xyArray = coordsConvert.ConvertToLocal(busLine.lat, busLine.lng);
                    busLine.x = xyArray[0];
                    busLine.y = xyArray[1];
                    points.push(busLine);
                }));
                var jsonData = points;
                let str = `count,lng,lat,x,y\n`;
                //增加\t为了不让表格显示科学计数法或者其他格式
                for (let i = 0; i < jsonData.length; i++) {
                    for (let item in jsonData[i]) {
                        str += `${jsonData[i][item] + '\t'},`;
                    }
                    str += '\n';
                }
                //encodeURIComponent解决中文乱码
                let uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(str);
                //通过创建a标签实现
                var link = document.createElement("a");
                link.href = uri;
                //对下载的文件命名
                link.download = $("#jsonName").val().split('.')[0] + ".csv";
                document.body.appendChild(link);
                link.click();
                document.body.removeChild(link);
            });

        }
    </script>
</body>

</html>